<?php

/**
 * valores actions.
 *
 * @package    SistemaWebUnlSenescyt
 * @subpackage valores
 * @author     JFernando
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class valoresActions extends sfActions {

    public function executeGraba2(sfWebRequest $request) {

        if (isset($_POST['id']) && isset($_POST['firstname']) && isset($_POST['lastname'])) {
            $id = mysql_escape_String($_POST['id']);
            $firstname = mysql_escape_String($_POST['firstname']);
            $lastname = mysql_escape_String($_POST['lastname']);
            $q = Doctrine_Query::create()
                    ->from('descripciongeneral d')
                    ->where('d.id_dg = ?', $id);
            $de = $q->fetchOne();
            $de->setCodigo($firstname);
            $de->setNombre($lastname);
            $de->save();
        }
    }

    public function executeGraba(sfWebRequest $request) {
        $servidor = "localhost";
        $usuario = "root";
        $clave = "";
        $basedatos = "sistemawebunlsenescyt";
        $conexion = mysql_connect($servidor, $usuario, $clave) or die(mysql_error());
        mysql_select_db($basedatos, $conexion) or die(mysql_error());

        if (isset($_POST['id']) && isset($_POST['firstname'])) {
            $id = mysql_escape_String($_POST['id']);
            $firstname = mysql_escape_String($_POST['firstname']);
            $sql = "update valor set val='$firstname' where id_va='$id'";
            mysql_query($sql);
        }
    }

    public function executeIndex(sfWebRequest $request) {
        $this->proyectos = Doctrine_Core::getTable('proyecto')
                ->createQuery('a')
                ->execute();        
    }

    public function executeYa(sfWebRequest $request) {
        $this->servidor = "localhost"; // host
        $this->usuario = "root";
        $this->clave = "";
        $this->basedatos = "sistemawebunlsenescyt";

        $this->idf = $request->getParameter('id_fo');

        $q = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo=?', $this->idf);

        $this->fondo = $q->fetchOne();

        $this->getUser()->setAttribute('fondoaux', $this->fondo->getIdFo());
        $this->getUser()->setAttribute('proy', $this->fondo->getIdPro());


        $this->objetivos = Doctrine::getTable('objetivo')->createQuery('o')
                ->where('o.id_pro = ?', $this->getUser()->getAttribute('proy'))
                ->execute();

        $this->techospresupuestarioss = Doctrine_Core::getTable('techos')->createQuery('t')
                ->where('t.id_pro = ?', $this->fondo->getIdPro())
                ->execute();



        $valoresfondo = Doctrine::getTable('valor')->createQuery('v')
                ->where('v.id_fo = ?', $this->idf)
                ->execute();


        $this->fonprova = 0;
        foreach ($valoresfondo as $valo):
            $this->fonprova = $this->fonprova + $valo->getVal();
        endforeach;
    }

    public function executeShow(sfWebRequest $request) {
        $this->valor = Doctrine_Core::getTable('valor')->find(array($request->getParameter('id_va')));
        $this->forward404Unless($this->valor);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new valorForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new valorForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($valor = Doctrine_Core::getTable('valor')->find(array($request->getParameter('id_va'))), sprintf('Object valor does not exist (%s).', $request->getParameter('id_va')));
        $this->form = new valorForm($valor);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($valor = Doctrine_Core::getTable('valor')->find(array($request->getParameter('id_va'))), sprintf('Object valor does not exist (%s).', $request->getParameter('id_va')));
        $this->form = new valorForm($valor);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($valor = Doctrine_Core::getTable('valor')->find(array($request->getParameter('id_va'))), sprintf('Object valor does not exist (%s).', $request->getParameter('id_va')));
        $valor->delete();

        $this->redirect('valores/index');
    }

    public function executeModelo(sfWebRequest $request) {
        $this->idf = $request->getParameter('id_fo');

        $fq = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo = ?', $this->idf);
        $this->faux = $fq->fetchOne();


        $this->fondosproyecto = Doctrine::getTable('fondo')->createQuery('f')
                ->where('f.id_pro = ?', $this->faux->getIdPro())
                ->execute();

        $pro = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('p.id_pro = ?', $this->faux->getIdPro());
        $this->proyecto = $pro->fetchOne();



        $this->valoresfondo = Doctrine::getTable('valor')->createQuery('v')
                ->where('v.id_fo = ?', $this->faux->getIdFo())
                ->execute();
        $s1 = 0;
        foreach ($this->valoresfondo as $val1):
            $s1 = $s1 + $val1->getVal();
        endforeach;
        $this->valido = false;
        if ($s1 > 0) {
            $this->valido = true;
            $this->valor = $s1;
        } else {
            $this->valido = false;
        }
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $valor = $form->save();
            $this->getUser()->setFlash('notice', 'Valor guardado satisfactoriamente');
        }
    }

}
